﻿using System;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Scheduler;

namespace FinalCountdownServices
{
    public static class AgentManager
    {
        private const String AgentName = "FinalCountdown.TileManager";
        private const String AgentDescription = "TileManager";

        public static void AddAgent()
        {
            var task = new PeriodicTask(AgentName)
            {
                Description = AgentDescription,
                ExpirationTime = DateTime.Now.AddDays(1)
            };

            if (ScheduledActionService.Find(AgentName) != null)
                ScheduledActionService.Remove(AgentName);

            try
            {
                ScheduledActionService.Add(task);

#if DEBUG
                RunDebugAgent();
#endif

            }
            catch
            {
                //Ha nem sikerült, valszeg le van tiltva
            }

        }
        public static void RemoveAgent()
        {
            if (ScheduledActionService.Find(AgentName) != null)
                ScheduledActionService.Remove(AgentName);
        }

        public static void RunDebugAgent()
        {

#if DEBUG

            Debug.WriteLine("Running Debug Agent");
            try
            {
                ScheduledActionService.LaunchForTest(AgentName, TimeSpan.FromSeconds(2));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
#endif

        }
        public static ScheduledAction FindAgent()
        {
            return ScheduledActionService.Find(AgentName);
        }

    }
}
